﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AGRTraining.za.co.agrtraining.model;

namespace AGRTraining.za.co.agrtraining.repository.Impl
{
    public class StudentDAO : IDAO<Student>
    {
        private AGRModelContainer agrDb = new AGRModelContainer();

        public void persist(Student entity)
        {
            agrDb.Students.AddObject(entity);
        }

        public void remove(Student entity)
        {
            agrDb.Students.DeleteObject(entity);
        }

        public void removeById(string code)
        {
            foreach (Student student in agrDb.Students.Where(s => s.StudentCode == code))
            {
                agrDb.Students.DeleteObject(student);
            }
        }

        public void merge(Student entity)
        {
            foreach (Student student in agrDb.Students.Where(s => s.StudentCode == entity.StudentCode))
            {
                student.StudentIDNumber = entity.StudentIDNumber;
                student.StudentFirstName = entity.StudentFirstName;
                student.StudentLastName = entity.StudentLastName;
                student.StudentGender = entity.StudentGender;
                student.StudentPostalAddress = entity.StudentPostalAddress;
                student.StudentPhysicalAddress = entity.StudentPhysicalAddress;
                student.StudentProvince = entity.StudentProvince;
                student.StudentNationality = entity.StudentNationality;
                student.StudentFirstLanguage = entity.StudentFirstLanguage;
                student.StudentSecondLanguage = entity.StudentSecondLanguage;
                student.StudentEquity = entity.StudentEquity;
                student.StudentResidentialStatus = entity.StudentResidentialStatus;
                student.StudentDisability = student.StudentDisability;
                student.StudentSocioStatus = entity.StudentSocioStatus;
            }
    
            //Select ALL
            //var students = (from s in agrDb.Students
            //                where s.StudentCode == "dsfsdf"
            //                select s);

            //Select Certain Columns
            //var students2 = (from s in agrDb.Students
            //                select new
            //                {
            //                    s.StudentCourses,
            //                    s.StudentDisability
            //                });

            //Order rows
            //var orderedListByCourses = students.OrderBy(s => s.StudentCourses);

        }

        public Student findById(string code)
        {
            Student student = new Student();

            foreach (Student stud in agrDb.Students.Where(s => s.StudentCode == code))
            {
                student = stud;
            }

            return student;
        }

        public IEnumerable<Student> findAll()
        {
            var students = (from stud in agrDb.Students
                            select stud);

            return students;
        }

        public IEnumerable<Student> findInRange(int firstResult, int maxResuslts)
        {
            var students = (from stud in agrDb.Students
                            select stud);

            return students.Skip(firstResult).Take(maxResuslts);
        }

        public long count()
        {
            var students = (from stud in agrDb.Students
                            select stud).ToList();

            return students.Count();
        }

        public void commit()
        {
            try
            {
                agrDb.SaveChanges();
            }
            catch
            {
               
            }
        }
    }
}